package com.example.cloudindex.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.example.cloudindex.bean.User;
import com.example.cloudindex.web.feign.OrderAction;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RefreshScope
public class IndexDo {

    @RequestMapping("index")
    public String index(HttpServletRequest request){
        final int port = request.getServerPort();
        return "index : " + port;
    }

    @Resource
    RestTemplate restTemplate;

    @RequestMapping("user")
    public String user(){
        return restTemplate.getForObject(
                "http://cloud-user/user", String.class);
    }

    @HystrixCommand(fallbackMethod="myOrder")
    @RequestMapping("order")
    public String order(){
        return restTemplate.getForObject(
                "http://cloud-order/order", String.class);
    }

    public String myOrder(){
        return "服务熔断: order";
    }

    @Resource
    OrderAction orderAction;
    @RequestMapping("orderByFegin")
    public String orderByFegin(){
        return orderAction.order();
    }

    @RequestMapping("sayHello")
    public String sayHello(String name){
        return orderAction.hello(name);
    }

    @RequestMapping("myname")
    public String myname(String name){
        return orderAction.myname(name);
    }

    @RequestMapping("add/{x}/{y}")
    public int add(
            @PathVariable("x") int a,
            @PathVariable("y")int b){
        return orderAction.add(a,b);
    }

    @RequestMapping("users")
    public User showUser(User user){
        return orderAction.displayUser(user);
    }

    @RequestMapping("showUser")
    public User showUser1(User user){
        return orderAction.showUser(user);
    }

    @Value("${myinfo.name}")
    String name;
    @Value("${myinfo.pwd}")
    String pwd;

    @RequestMapping("MyInfo")
    public String myinfo(){
        return name + " : " + pwd;
    }

}
